package com.self.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-05-31 10:48
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class CountDownLatchTest {

    public static void main(String[] args) {
        //线程计数器 count 值不可修改
        final CountDownLatch countDownLatch = new CountDownLatch(2);

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("first sub thread start");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                System.out.println("first sub thread end");
            }
        }).start();

        new Thread(){
            @Override
            public void run() {
                System.out.println("second sub thread start");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                System.out.println("second sub thread end");
            }
        }.start();


        try {
            System.out.println("main thread await");
            countDownLatch.await();
            System.out.println("main thread continue");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
